# -*- coding: utf-8-*-
from scrapy import Request
from scrapy.spiders import Spider

from tmail.items import TmailItem

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

class TmailSpider(Spider):
    name = 'tmail.com'
    allow_domains = ['tmail.com']
    start_urls = [
        'https://list.tmall.com/search_product.htm?cat=51454011&sort=td&acm=lb-zebra-27092-331834.1003.8.457096&style=g&industryCatId=51462017&active=1&user_id=725677994&scm=1003.8.lb-zebra-27092-331834.ITEM_14434945515601_457096#J_Filter'
    ] 
    
    def start_requests(self):
        cookies = { 'sm4':'320100', 
            'cna':'iLFCEBO5uiECAd9BvNbhzyQF',
            'l':'Ao7XRbF8lzRWbHVuuiZ-1zub/j5H0I',
            'isg':'AsKvrvXJkXgdgVVAF7uHKbVXIM55uuAqvSZ5m00dZ9COfKoZwr_gWKzHOv', 
            '_med':'dw:640&dh:480&pw:640&ph:480&ist:0',
            'cq':'cicp%3D0',
            'pnm_cku822':'180UW5TcyMNYQwiAiwTR3f0J%2FQnhEcUpkMmQ%3D%7CUm5OcktxTnRLdkJ5QXpPciQ%3D%7CU2xMHDJ%2BH2QJZwBxX39RaFF%2FX3E3VjBMPRNFEw%3D%3D%7CVGhXd1llXGZZY1xhVW5WbVhlUm9Nc0t%2BSn5DfEF4QH1Gf0RxTWM1%7CVWldfS0TMw8wDCwQKwslVGoHMhsmGCMZMwY4HStFIgxaDA%3D%3D%7CVmhIGCwUNA83Fy8VLQ03CjQOLhUsETEIPQAgGCIaOgM7AFYA%7CV25Tbk5zU2xMcEl1VWtTaUlwJg%3D%3D',
            'hng':'', 
            'uss':'BdM3qIJ10DFP03ajG9yOHvScJcixZ%2FnjTu9IuQDIi6863W%2BDpC6sa4gQ%3D%3D',
            'otherx':'e%3D1%26p%3D*%26s%3D06c%3D0%26f%3D0%26g%3D0%26t%3D0',
            'x':'__ll%3D-1%26_ato%0',
            't':'3a9d77bfaa8c7b6a2c1a45c6c95242', 
            'uc3':'sg2=WqTDFDaak01vu8HFXJ6rxk9Gpc9w23VjqqHVMbPYw%3D&nk2=rpd1TWm2c0U%3D&id2=UonfNOZuNuZbbg%3D%3D&vt3=F8dAS1OmkS8UQ8BHHEY%3D&lg2=U%2BGCWk%2F75gdr5Q%3D%3D',
            'lgc':'%5Cu4E07%u4E07%5Cu788E%5Cu955C',
            'tracknick':'%u4E07%5Cu4E07%5Cu788E%5Cu955C', 
            '_tb_token_':'TpsgZB4mTaB',
            'cookie2':'d86a462aaec78b9a4b645833e228e0',
            'ck1':'', 
            'uc1':'cooe14=UoWwJryDvq2Q3Q%3D%3D&lng=zh_CN&cookie16=WqG3DMC9UpAPBHGz5QBErFxlCA%3D%3D&existShop=false&cookie21=UtASsssmeW6lpyd%2BB%2B3t&tag=7&cookie15=VFC%2FuZ9ayeYq2g%3D%3D&pas=0',
            'cookie1':'xT7eQrkWYmXhOmWRUW%2BiQAuFmrm6VD9SLHn%2FQrB4JE%3D',
            'unb':'184723601',
            'skt':'d175cb3f040dbfb',
            '_nk_':'%5u4E07%5Cu4E07%5Cu788E%5Cu955C',
            '_l_g_':'g%3D%3D',
            'cookie17':'UnfNOZuNuZbbg%3D%3D',
            'login':'tru', 
            'tt':'logitaobao.com'
    }
        
        for url in self.start_urls:
            yield Request(url, cookies = cookies, callback = self.parse)
        
    def parse(self, response):

        title = response.xpath("//h3[@class = 'product-title']/a/text()").extract()
        sold = response.xpath("//div[@class = 'item-sum']/strong/text()").extract()
        price = response.xpath("//div[@class = 'item-price']/span/strong/text()").extract()
        for i in range(0, len(sold)):
            item = TmailItem()
            item['title'] = title[i]
            item['sold'] = sold[i]
            item['price'] = price[i]
            yield item

        next_page = response.xpath("//div[@class = 'list-bottom']/div/div/a[@class = 'page-next']/@href").extract()[0]
        next_url = 'https://list.tmall.com/search_product.htm' + next_page
        yield Request(next_url, callback = self.parse)

